package com.onejson.ojmall.controller;

import com.onejson.ojmall.entity.ProductEntity;
import com.onejson.ojmall.entity.dto.ProductDTO;
import com.onejson.ojmall.entity.vo.ProductVO;
import com.onejson.ojmall.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/product")
@Api(tags = "商品管理")
public class ProductController {

    @Resource
    private IProductService productService;

    @ApiOperation("获取商品列表")
    @GetMapping("/list")
    public List<ProductVO> list(ProductEntity product) {
        return productService.getProductList(product);
    }

    @ApiOperation("获取商品详情")
    @GetMapping("/{id}")
    public ProductVO getInfo(@PathVariable("id") Long id) {
        return productService.getProductById(id);
    }

    @ApiOperation("新增商品")
    @PostMapping
    public boolean add(@Validated @RequestBody ProductDTO productDTO) {
        return productService.addProduct(productDTO);
    }

    @ApiOperation("修改商品")
    @PutMapping
    public boolean update(@Validated @RequestBody ProductDTO productDTO) {
        return productService.updateProduct(productDTO);
    }

    @ApiOperation("删除商品")
    @DeleteMapping("/{ids}")
    public boolean delete(@PathVariable Long[] ids) {
        return productService.deleteProducts(ids);
    }

    @ApiOperation("商品上架")
    @PutMapping("/{id}/online")
    public boolean online(@PathVariable("id") Long id) {
        return productService.onlineProduct(id);
    }

    @ApiOperation("商品下架")
    @PutMapping("/{id}/offline")
    public boolean offline(@PathVariable("id") Long id) {
        return productService.offlineProduct(id);
    }
} 